import Vue from 'vue'
import {Format,parseDate} from '@/common/js/dateFormat'
import bus from '@/components/common/bus'
import enumService from './enumService'
import { getUserButtonMenus } from '../../api/sys/user'

Date.prototype.Format = Format;

let minxin = {
  methods: {
    audioBeforeUpload(file){
      const isAudio = (file.type === 'audio/mp3');
      const isLt5M = file.size / 1024 / 1024 < 50;
      if (!isAudio) {
        this.$message.error('上传音频只能是 mp3');
      }
      if (!isLt5M) {
        this.$message.error('上传音频大小不能超过 5MB!');
      }
      return isAudio && isLt5M;
    },
    videoBeforeUpload(file){
      const isVideo = (file.type === 'video/mp4');
      const isLt5M = file.size / 1024 / 1024 < 5;
      if (!isVideo) {
        this.$message.error('上传视频只能是 mp3');
      }
      if (!isLt5M) {
        this.$message.error('上传视频大小不能超过 5MB!');
      }
      return isVideo && isLt5M;
    },
    beforeSingleAvatarUpload(file) {
      const isJPG = (file.type === 'image/jpeg') || (file.type === 'image/png');
      const isLt2M = file.size / 1024 / 1024 < 2;
      if (!isJPG) {
        this.$message.error('上传图片只能是 JPG 或 PNG 格式!');
      }
      if (!isLt2M) {
        this.$message.error('上传图片大小不能超过 2MB!');
      }
      return isJPG && isLt2M;
    },
    addTag(title,fullPath,name){
      bus.$emit('addTag',{fullPath: fullPath, title: title, name: name})
    },
    closeTagByName(name){
      bus.$emit('closeTagByName',name);
    },
    checkAuth(auths){
      let authsData = this.$store.state.authsData;
      if(authsData && authsData.length > 0){
        let flag = false;
        authsData.forEach(item=>{
          if(item === auths){
          flag = true;
          return;
        }
      })
        return flag;
      }else{
        return false;
      }
    },
    //格式化日期
    renderTime(time) {
      if(time){
        time = time.toString();
        if(time.length === 10){
          time = (time-0) * 1000;
        }
        time = (time - 0);
        if (time) {
          return (new Date(time)).Format("yyyy-MM-dd hh:mm:ss");
        } else {
          return "--";
        }
      }
    },
    renderTimeWithOutYear(time) {
      if(time){
        time = time.toString();
        if(time.length === 10){
          time = (time-0) * 1000;
        }
        time = (time - 0);
        if (time) {
          return (new Date(time)).Format("MM-dd hh:mm");
        } else {
          return "--";
        }
      }
    },
    //格式化日期
    renderTimeNoTime(time) {
      time = (time - 0);
      if (time) {
        return (new Date(time)).Format("yyyy-MM-dd");
      } else {
        return "--";
      }
    },
    //格式化日期
    renderTimeMonthDay(time) {
      time = (time - 0);
      if (time) {
        return (new Date(time)).Format("MM月dd日");
      } else {
        return "--";
      }
    },
    renderTime_str2date(datestr,fmt){
      if(datestr){
        if(!fmt) fmt = 'yyyy-MM-dd hh:mm:ss';
        return this.renderTime(parseDate(datestr,fmt))
      }else{
        return "--"
      }
    },
    getTodayTime(){
      debugger
      let today = new Date();
      let todayStr = this.renderTimeNoTime(today.getTime());
      let fmt = 'yyyy-MM-dd hh:mm:ss';
      return parseDate(todayStr,fmt).getTime();
    },
    resetForm(formName) {
      if (this.$refs[formName]) this.$refs[formName].resetFields();
    },
    handleClose() {
      this.$emit('handleClose');
    },
    handleClose(closeFunc) {
      this.$emit(closeFunc);
    },
    enumMap(enumName, packagePath) {
      if (packagePath) {
        return enumService.getEnumArrayPlus(packagePath, enumName);
      } else {
        return enumService.getEnumArray(enumName);
      }
    },
    enumMapDesc(enumName, value, packagePath) {
      let res = '--';
      let dict = {};
      if (packagePath) {
        dict = enumService.getEnumArrayPlus(packagePath, enumName);
      } else {
        dict = enumService.getEnumArray(enumName);
      }
      Object.keys(dict).find(key => {
        if (value == key.toString()) {
          res = dict[key];
        }
      });
      return res;
    },
    cellStyleAlignCenter: function (row, column, rowIndex, columnIndex) {
      return 'text-align:center'
    },
  }
};

Vue.mixin(minxin)
